//
//  MainViewController.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 16/1/1.
//  Copyright © 2016年 张鹏飞. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //MARK: - 监听方法
  @objc private func clickComposeButton(){
    
        print("点我了")
    
    }
//MARK: - 视图生命周期方法
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        setUpComposeButton()
    }
   
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
    }
    
    //MARK: - 懒加载控件
    private lazy var composeButton:UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
  
}
//MARK: - 设置控件
extension MainViewController{
    
    //设置撰写按钮
    private func setUpComposeButton(){
    
        //1,添加按钮
        tabBar.addSubview(composeButton)
        
        //2.调整按钮
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat (count) - 1
        
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        //3.添加监听方法
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    }

    private func addChildViewControllers(){
        
        //tintColor--图片渲染颜色
        tabBar.tintColor = UIColor.orangeColor()
    
        addChildViewController(HomeTableViewController(), title: "首页", ImageName: "tabbar_home")
        
         addChildViewController(MessageTableViewController(), title: "消息", ImageName: "tabbar_message_center")
        
        //站位
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", ImageName: "tabbar_discover")
        
        addChildViewController(MessageTableViewController(), title: "我", ImageName: "tabbar_profile")
    }
    //添加控制器
    private func addChildViewController(vc:UIViewController, title:String, ImageName: String) {
        
//        let vc = vc
        
        vc.title = title
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: ImageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
       self.addChildViewController(nav)
        
      
    }
}
